//
//  NewsViewController.m
//  projectA_kuaiZhui
//
//  Created by 张祎 on 15-3-21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsTableViewCell.h"
#import "NewsDetailViewController.h"
#import "News.h"
#import "NetHandler.h"
#import "UIImageView+WebCache.h"
@interface NewsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arr;
@end

@implementation NewsViewController
//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    self.navigationController.navigationBarHidden = YES;
//}
- (void)handleData{
    
    self.arr = [NSMutableArray array];
    NSString *str = @"http://api.zhuizhuiyoyo.com/request.php?method=theme_info&timestamp=1426770037838&param={\"id\":\"148\",\"v\":1,\"t\":0,\"count\":18,\"page\":1}&sig=00a727da3e1c753ea0ed34b0ebf62ce7";
    [NetHandler getDataWithUrl:str completion:^(NSData *data) {
        
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
       
       NSMutableArray *arr = [result objectForKey:@"theme_list"];
     
        
        for (NSMutableDictionary *dic in arr) {
            News *news = [[News alloc]initWithDictionary:dic];
            if ([news.url hasPrefix:@"http"]) {
                [self.arr addObject:news];
              
            }
            [news release];
        }
           [self.tableView reloadData];
    }];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self handleData];
    self.title = @"快追资讯";
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 240;
  
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    [self.tableView registerClass:[NewsTableViewCell class] forCellReuseIdentifier:@"reuse"];
 
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count ;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" ];
    
    News *new = [self.arr objectAtIndex:indexPath.row];
    
    cell.news = new;
 
    return cell;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NewsDetailViewController *newView = [[NewsDetailViewController alloc]init];
    News *news = [self.arr objectAtIndex:indexPath.row];
    newView.news = news;
    [self.navigationController pushViewController:newView animated:YES];
    [newView release];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
